package com.cloudptt.wp.ui.message.adapter;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.media.MediaPlayer;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.cloudptt.api.comm.CheckData;
import com.cloudptt.api.comm.CommDlg;
import com.cloudptt.api.comm.Common;
import com.cloudptt.api.comm.FileOperation;
import com.cloudptt.api.product.Product;
import com.cloudptt.api.product.ProductConst;
import com.cloudptt.api.product.log.Log;
import com.cloudptt.api.product.operator.MsgOperator;
import com.cloudptt.wp.R;
import com.cloudptt.wp.SysApplication;
import com.cloudptt.wp.ui.dlg.UiDlg;
import com.cloudptt.wp.ui.message.ForwardMsgActivity;
import com.cloudptt.wp.ui.message.ImageActivity;
import com.cloudptt.wp.ui.message.MessagesStatusActivity;
import com.cloudptt.wp.ui.message.SessionActivity;
import com.cloudptt.wp.ui.message.component.AudioPlayer;
import com.mcptt.config.body.info.mcdata.FileMataInfo;
import com.bumptech.glide.request.target.Target;
import com.mcptt.db.model.AddrInfo;
import com.mcptt.db.model.SessionInfo;

import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;


public class SessionAdapter extends BaseAdapter implements DialogInterface.OnClickListener, MediaPlayer.OnCompletionListener
{
	public static final int Max_Heigh                          = 300;
	private Context ctx = null;
	private AddrInfo curAddrInfo = null;
	private List<SessionInfo> list = null;
	//	private ProgressDialog dlg;
	private IDownload download = null;
	private IUptUi uptUi = null;
	private int curPos = 0;
	private Boolean isMulSelected = false;
	private List<MulSelectedIndex> mulSelectList = new Vector<>();
	private AnimationDrawable animation = null;
//	private int playVoicePos = -1;
//	private boolean isPlayVoice = false;
	private ImageView playVoiceView = null;
	private int playVoiceDir = SessionInfo.Direction_Type_Send;

	public SessionAdapter(Context ctx,AddrInfo curUserInfo,List<SessionInfo> list,IDownload download,IUptUi uptUi)
	{
		this.ctx = ctx;
		this.curAddrInfo = curUserInfo;
		this.list = list;
		this.download = download;
		this.uptUi = uptUi;
	}
	public void refreshData(List<SessionInfo> list)
	{
		this.list = list;
	}
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder;

		SessionInfo info = list.get(position);
		Log.debug(getClass(), "getView info = " + info);
		int direction = info.getDirection();
		int status = info.getStatus();
		if (status != SessionInfo.Status_SDS_READ
				&& SessionInfo.Direction_Type_Receive == direction)
		{
			int dispositionReqType = info.getDispositionReqType();
			if((dispositionReqType & SessionInfo.READ) > 0)
			{
				SysApplication app = ((SysApplication) ctx.getApplicationContext());
				Product product = app.getProduct();
				MsgOperator operator = (MsgOperator)product.getOperator(ProductConst.Func_Msg);
				operator.sendNtfMessage(info,SessionInfo.Status_SDS_READ);
			}
		}
		Log.debug(getClass(), "getView convertView = " + convertView);
//		if (convertView == null)
		{
			LayoutInflater inflater = LayoutInflater.from(ctx);
			Log.debug(getClass(), "getView direction = " + direction);
			if (SessionInfo.Direction_Type_Send == direction)
				convertView = inflater.inflate(R.layout.chatting_item_msg_send, null);
			else
				convertView = inflater.inflate(R.layout.chatting_item_msg_recv, null);

			viewHolder = new ViewHolder();
			viewHolder.mulSelectedCheckBox = (CheckBox) convertView.findViewById(R.id.mulSelectedCheckBox);
			viewHolder.imgView = (ImageView) convertView.findViewById(R.id.iv_userhead);
			viewHolder.tvUserName = (TextView) convertView
					.findViewById(R.id.tv_username);
			viewHolder.tvSendTime = (TextView) convertView
					.findViewById(R.id.tv_sendtime);
//			viewHolder.funcTitleTv = (TextView) convertView
//					.findViewById(R.id.tv_func_title);
			viewHolder.contentLy = (FrameLayout) convertView
					.findViewById(R.id.contentLy);
			viewHolder.textTv = (TextView) convertView
					.findViewById(R.id.textTv);
			viewHolder.imageIv = (ImageView) convertView
					.findViewById(R.id.imageIv);

			viewHolder.voiceLy = (LinearLayout) convertView
					.findViewById(R.id.voiceLy);
			viewHolder.voiceIv = (ImageView) convertView
					.findViewById(R.id.voiceIv);
			viewHolder.voiceLenTv = (TextView) convertView
					.findViewById(R.id.voiceLenTv);
			viewHolder.voiceListenedTv = (TextView) convertView
					.findViewById(R.id.voiceListenedTv);

			viewHolder.fileLy = (LinearLayout) convertView
					.findViewById(R.id.fileLy);
			viewHolder.fileNameTv = (TextView) convertView
					.findViewById(R.id.fileNameTv);
			viewHolder.fileSizeTv = (TextView) convertView
					.findViewById(R.id.fileSizeTv);


			viewHolder.statusIv = (ImageView) convertView
					.findViewById(R.id.stateIv);
			convertView.setTag(viewHolder);
		}
//		else
//		{
//			viewHolder = (ViewHolder) convertView.getTag();
//		}
		Log.debug(getClass(), "getView isMulSelected = " + isMulSelected);
		if(isMulSelected)
		{
			viewHolder.mulSelectedCheckBox.setVisibility(View.VISIBLE);
			if(hasMulSelectIndex(position))
			{
				viewHolder.mulSelectedCheckBox.setChecked(true);
			}
			else
			{
				viewHolder.mulSelectedCheckBox.setChecked(false);
			}

			viewHolder.mulSelectedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
				@Override
				public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
					if(isChecked)
					{
						MulSelectedIndex index = new MulSelectedIndex(position);
						mulSelectList.add(index);
					}
					else
					{
						removeMulSelectIndex(position);
					}
				}
			});
		}
		else
		{
			viewHolder.mulSelectedCheckBox.setVisibility(View.GONE);
		}
		AddrInfo addrInfo = info.getSenderAddrInfo();
		if(addrInfo.isSys()
				|| addrInfo.isDac())
		{
			viewHolder.imageIv.setImageResource(R.drawable.icon_main_user_dac_face);

		}else if(addrInfo.isFuncType())
		{
			viewHolder.imageIv.setImageResource(R.drawable.icon_main_user_func_face);

		}
		else{
			viewHolder.imageIv.setImageResource(R.drawable.icon_main_user_face);
		}

		if (SessionInfo.Direction_Type_Send == direction)
		{
//			String val = Resource.getMcdtataStatusString(info.isFdMsg(),status);
//			Log.debug(getClass(), "getView status = " + status + " val = " + val);
			if(info.isShowStatus()) {
				Drawable drawable = getMcdtataStatusDrawable(info.isFdMsg(),status);
				viewHolder.statusIv.setBackground(drawable);
				viewHolder.statusIv.setVisibility(View.VISIBLE);
			}
			else
			{
				viewHolder.statusIv.setVisibility(View.INVISIBLE);
			}
			if(!info.isOtoMsg())
			{
				viewHolder.statusIv.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// 在这里，position就是点击的控件所在的行
						// 你可以在这里处理点击事件，例如弹出Toast或者跳转到另一个Activity
						doViewClick(R.id.stateIv,position);
					}
				});
			}
		}
		if(curAddrInfo != null
				&& this.curAddrInfo.isGroupType())
		{
			viewHolder.tvUserName.setVisibility(View.VISIBLE);
			String name = info.getSenderAddrInfo().getDscp();
			viewHolder.tvUserName.setText(name);
		}
		else
		{
			viewHolder.tvUserName.setVisibility(View.GONE);
		}
//		if(info.isFuncMsg())
//		{
//			String peerNum = info.getPeerAddrInfo().getAddrId();
//			String sendernum = info.getSenderAddrInfo().getAddrId();
//			String resultStr = ctx.getString(R.string.func_msg_title, peerNum,sendernum);
//			Log.debug(this.getClass(),"onQueryResult resultStr = " + resultStr);
//			viewHolder.funcTitleTv.setText(resultStr);
//			viewHolder.funcTitleTv.setVisibility(View.VISIBLE);
//		}
//		else {
//			viewHolder.funcTitleTv.setVisibility(View.GONE);
//		}

		viewHolder.tvSendTime.setText("  "+info.getDateStr()+"  ");
		int msgType = info.getContentType();
		Log.debug(getClass(), "getView msgType = " + msgType);

		if(msgType == SessionInfo.ContentType_FileURL)
		{
			viewHolder.textTv.setVisibility(View.GONE);
			String fileMataStr = info.getFileMetaData();
			Log.debug(getClass(), "getView fileMataStr = " + fileMataStr);
			FileMataInfo fileMataInfo = FileMataInfo.parse(fileMataStr);
			String name = fileMataInfo.getFileName();
			String type = fileMataInfo.getType();
			Log.debug(getClass(), "getView name = " + name + " type = " + type);

			if(FileOperation.isPicture(type))
			{
				if (SessionInfo.Direction_Type_Send == direction)
				{
					String localPath = info.getLocalPath();
					Log.debug(getClass(), "getView isPicture  send http localPath = " + localPath);
//					Picasso.with(ctx).load(path).resize(200, 200)
//							.onlyScaleDown().into(viewHolder.imageIv);
					File file = new File(localPath);
					Glide.with(ctx)
							.load(file)
							.override(Target.SIZE_ORIGINAL, Max_Heigh)
							.placeholder(R.drawable.placeholder)  // 加载中占位图
							.error(R.drawable.error_image)
							.into(viewHolder.imageIv);
//					Picasso.get().load("file://" + localPath).fit()          // 自动适配 ImageView 的尺寸
//							.centerInside() // 保持宽高比
//					 .into(viewHolder.imageIv);
				}
				else
				{
					String path = info.getContent();
					String localPath = info.getLocalPath();
					Log.debug(getClass(), "getView isPicture recv http path = " + path + " localPath = " + localPath);
					if(localPath != null
							&& localPath.equals("")) {
						Glide.with(ctx)
								.load(path)
								.override(Target.SIZE_ORIGINAL, Max_Heigh)
								.placeholder(R.drawable.placeholder)  // 加载中占位图
								.error(R.drawable.error_image)        // 失败时显示
//								.diskCacheStrategy(DiskCacheStrategy.ALL)
								.into(viewHolder.imageIv);
//						Picasso.get().load(path).fit().centerInside().into(viewHolder.imageIv);
					}
					else
					{
						File file = new File(localPath);
						Glide.with(ctx)
								.load(file)
								.override(Target.SIZE_ORIGINAL, Max_Heigh)
								.placeholder(R.drawable.placeholder)  // 加载中占位图
								.error(R.drawable.error_image)
								.into(viewHolder.imageIv);
//						Picasso.get().load("file://" + localPath).fit().centerInside().into(viewHolder.imageIv);
					}
				}

				viewHolder.textTv.setVisibility(View.GONE);
				viewHolder.imageIv.setVisibility(View.VISIBLE);
				viewHolder.voiceLy.setVisibility(View.GONE);
				viewHolder.fileLy.setVisibility(View.GONE);
			}
			else if (FileOperation.isAudio(type))
			{
				String path = info.getContent();
				Log.debug(getClass(), "getView isAudio path = " + path);
				viewHolder.textTv.setVisibility(View.GONE);
				viewHolder.imageIv.setVisibility(View.GONE);
				viewHolder.voiceLy.setVisibility(View.VISIBLE);
				String sizeStr = fileMataInfo.getVoiceSizeStr();
				Log.debug(getClass(), "getView isAudio  1 sizeStr = " + sizeStr);
				int size = (int)CheckData.parseFloat(sizeStr);
				Log.debug(getClass(), "getView isAudio  1 size = " + size);
				if(size <= 0)
				{
					size = 1;
				}
				Log.debug(getClass(), "getView isAudio  2 size = " + size);
				if(size > 0) {
					String fullWidthSpaces = Common.generateSpaces(size);
					if (SessionInfo.Direction_Type_Send == direction)
					{
						viewHolder.voiceLenTv.setText(fullWidthSpaces + "" + size + "\"");
					}
					else
					{
						viewHolder.voiceLenTv.setText("" + size + "\"" + fullWidthSpaces);
					}
				}
				String localPath = info.getLocalPath();
				Log.debug(getClass(), "getView isAudio http path = " + path + " localPath = " + localPath);

				if(localPath != null
						&& localPath.equals(""))
				{
					viewHolder.voiceListenedTv.setVisibility(View.VISIBLE);
				}
				else
				{
					viewHolder.voiceListenedTv.setVisibility(View.GONE);
				}
				viewHolder.fileLy.setVisibility(View.GONE);
			}
			else
			{
				String size = fileMataInfo.getSizeStr();
				Log.debug(getClass(), "getView file size = " + size);
				viewHolder.fileLy.setVisibility(View.VISIBLE);
				viewHolder.fileNameTv.setText(name);
				viewHolder.fileSizeTv.setText(size);

				viewHolder.textTv.setVisibility(View.GONE);
				viewHolder.imageIv.setVisibility(View.GONE);
				viewHolder.voiceLy.setVisibility(View.GONE);
			}

		}
		else
		{
			viewHolder.textTv.setVisibility(View.VISIBLE);
			viewHolder.imageIv.setVisibility(View.GONE);
			viewHolder.voiceLy.setVisibility(View.GONE);
			viewHolder.fileLy.setVisibility(View.GONE);
			String content = info.getContent();
			Log.debug(getClass(), "getView content = " + content);
			viewHolder.textTv.setText(content);
		}


		viewHolder.textTv.setOnLongClickListener(new View.OnLongClickListener(){
			public boolean onLongClick(View v)
			{
				doViewLongClick(position);
				return true;
			}
		});
		viewHolder.imageIv.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// 在这里，position就是点击的控件所在的行
				// 你可以在这里处理点击事件，例如弹出Toast或者跳转到另一个Activity
				doViewClick(R.id.imageIv,position);
			}
		});
		viewHolder.imageIv.setOnLongClickListener(new View.OnLongClickListener(){
			public boolean onLongClick(View v)
			{
				doViewLongClick(position);
				return true;
			}
		});
		viewHolder.voiceLy.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// 在这里，position就是点击的控件所在的行
				// 你可以在这里处理点击事件，例如弹出Toast或者跳转到另一个Activity
				doViewClick(R.id.voiceLy,position);
//				if(playVoicePos == position) {
//					isPlayVoice = !isPlayVoice;
//				}
//				else
//				{
//					isPlayVoice = true;
//				}
//				if(isPlayVoice) {
					startFrameAnimByXml(position,viewHolder.voiceIv, direction);
//				}
//				else
//				{
//					stopAnim();
//				}
			}
		});
		viewHolder.voiceLy.setOnLongClickListener(new View.OnLongClickListener(){
			public boolean onLongClick(View v)
			{
				doViewLongClick(position);
				return true;
			}
		});
		viewHolder.fileLy.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// 在这里，position就是点击的控件所在的行
				// 你可以在这里处理点击事件，例如弹出Toast或者跳转到另一个Activity
				doViewClick(R.id.fileLy,position);
			}
		});
		viewHolder.fileLy.setOnLongClickListener(new View.OnLongClickListener(){
			public boolean onLongClick(View v)
			{
				doViewLongClick(position);
				return true;
			}
		});

		return convertView;
	}
	private void startFrameAnimByXml(int pos,ImageView view,int direction) {
		stopAnim();
//		this.playVoicePos = pos;
		this.playVoiceView = view;
		this.playVoiceDir = direction;
		// 设置图像视图的图像来源为帧动画的XML定义文件
		if(SessionInfo.Direction_Type_Send == direction) {
			view.setImageResource(R.drawable.animation_list_msg_voice_send);
		}
		else
		{
			view.setImageResource(R.drawable.animation_list_msg_voice_recv);
		}
		// 从图像视图对象中获取帧动画
		animation = (AnimationDrawable) view.getDrawable();
		animation.start(); // 开始播放帧动画
	}
	public void stopAnim()
	{
		if(animation != null)
		{
			animation.stop();
		}
		if(playVoiceView != null) {
			if (SessionInfo.Direction_Type_Send == playVoiceDir) {
				playVoiceView.setImageResource(R.drawable.icon_voice_send);
			} else {
				playVoiceView.setImageResource(R.drawable.icon_voice_recv);
			}
		}
	}
	public Drawable getMcdtataStatusDrawable(Boolean isFd, int status)
	{
		if(isFd)
		{
			if(status == 3)
			{
				return ctx.getDrawable(R.drawable.msg_status_fd_3);
			}
			else if(status == 2)
			{
				return ctx.getDrawable(R.drawable.msg_status_fd_2);
			}
			else
			{
				return ctx.getDrawable(R.drawable.msg_status_fd_1);
			}
		}
		else
		{
			if(status == 3)
			{
				return ctx.getDrawable(R.drawable.msg_status_sds_3);
			}
			else if(status == 2)
			{
				return ctx.getDrawable(R.drawable.msg_status_sds_2);
			}
			else if(status == 1)
			{
				return ctx.getDrawable(R.drawable.msg_status_sds_1);
			}
			else
			{
				return ctx.getDrawable(R.drawable.msg_status_sds_0);
			}
		}
	}

	public void clearSelect(){

	}

	public void doViewClick(int id,int pos) {
		Log.debug(getClass(), "doViewClick id = " + id + " R.id.stateTv = " + R.id.stateIv + " pos = " + pos);
		if(id == R.id.imageIv
				||id == R.id.voiceLy
				|| id == R.id.fileLy)
		{
			SessionInfo info = list.get(pos);
			int msgType = info.getContentType();
			int direction = info.getDirection();
			String localPath = info.getLocalPath();
			Log.debug(getClass(), "onClick localPath = " + localPath);
			if (SessionInfo.Direction_Type_Send == direction
					&& id == R.id.imageIv)
			{
				if(localPath != null
						&& !localPath.equals(""))
				{
					Intent intent = new Intent();
					intent.setClass(ctx, ImageActivity.class);
					intent.putExtra("local_path",localPath);
					ctx.startActivity(intent);
				}
			}
			if (msgType == SessionInfo.ContentType_FileURL) {
				final String path = info.getContent();
				Log.debug(getClass(), "onClick path = " + path);
				String fileMataStr = info.getFileMetaData();
				Log.debug(getClass(), "getView fileMataStr = " + fileMataStr);
				FileMataInfo fileMataInfo = FileMataInfo.parse(fileMataStr);
				String name = fileMataInfo.getFileName();
				Log.debug(getClass(), "getView name = " + name);

				if (FileOperation.isAudio(name))
				{
//					AudioPlayer.playSound(ctx, path, null);
//					download.sendNtfMessage(info,SessionInfo.Status_FD_COMPLETED);
					if(localPath != null
							&& localPath.equals(""))
					{
						if(download != null) {
							download.showDownloadDialog(info);
						}
					}
					else if(id == R.id.voiceLy
							&& localPath != null
							&& !localPath.equals(""))
					{
//						AudioPlayer.playSound(ctx, path, null);
						playVoiceMsg(info);
					}
				} else {
					if(localPath != null
							&& localPath.equals(""))
					{
						if(download != null) {
							download.showDownloadDialog(info);
						}
					}
					else if(id == R.id.imageIv
							&& localPath != null
							&& !localPath.equals(""))
					{
						Intent intent = new Intent();
						intent.setClass(ctx, ImageActivity.class);
						intent.putExtra("local_path",localPath);
						ctx.startActivity(intent);
					}
				}
			}
		}
		else if(id == R.id.stateIv)
		{
			SessionInfo info = list.get(pos);
			if(!info.isOtoMsg())
			{
				Intent intent = new Intent();
				intent.setClass(ctx, MessagesStatusActivity.class);
				intent.putExtra(SessionActivity.KEY,info);
				//		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
				ctx.startActivity(intent);
			}
		}
	}

	public void playVoiceMsg(SessionInfo info)
	{
		final String path = info.getLocalPath();
		Log.debug(getClass(), "playVoiceMsg path = " + path);
		AudioPlayer.playSound(ctx, path, this);
	}
	@Override
	public void onCompletion(MediaPlayer mediaPlayer) {
		stopAnim();
//		this.isPlayVoice = false;
//		this.playVoicePos = -1;
	}
	public void doViewLongClick(int pos) {
		Log.debug(getClass(), "doViewLongClick pos = " + pos);
//		if(R.id.userLy == id)
		{
			this.curPos = pos;
			SessionInfo info = list.get(pos);

			MulSelectedIndex index = new MulSelectedIndex(pos);
			this.mulSelectList.add(index);
			String item1 = ctx.getString(R.string.session_forward);
			String item2 = ctx.getString(R.string.session_del);
			String item3 = ctx.getString(R.string.op_msg_mul_select);
			String item4 = ctx.getString(R.string.session_copy);
			if(!info.isFdMsg())
			{
				String[] list = new String[]{item1,item2,item3,item4};
				UiDlg.showListDialog(ctx, list, this);
			}
			else
			{
				String[] list = new String[]{item1,item2,item3};
				UiDlg.showListDialog(ctx, list, this);
			}
		}
	}

	@Override
	public void onClick(DialogInterface dialogInterface, int index)
	{
		switch (index) {
			case 0:
				doForwardAction();
				break;
			case 1:
				doDelAction();
				break;
			case 2:
				doMulSelectedAction();
				break;
			case 3:
				doCopyAction();
				break;
			default:
				break;
		}
	}
	private void doForwardAction()
	{
		SessionInfo info = this.list.get(curPos);
		ArrayList<SessionInfo> forwardList = new ArrayList<SessionInfo>();
		forwardList.add(info);

		mulSelectList.clear();
		curPos=0;

		forwardAction(forwardList);
	}
	public void doForwardMulAction()
	{
		if(mulSelectList != null
				&& mulSelectList.size() > 0)
		{
			Collections.sort(mulSelectList);
			Log.debug(this.getClass(),"doForwardMulAction mulSelectList = " + mulSelectList);
			ArrayList<SessionInfo> forwardList = new ArrayList<SessionInfo>();
			for(Iterator<MulSelectedIndex> itor = mulSelectList.iterator();itor.hasNext();)
			{
				MulSelectedIndex index = itor.next();
				Log.debug(this.getClass(),"doForwardMulAction index = " + index);
				SessionInfo info = this.list.get(index.getIndex());
				forwardList.add(info);
			}
			mulSelectList.clear();
			curPos=0;

			forwardAction(forwardList);
			this.isMulSelected = false;
			if(uptUi != null)
			{
				uptUi.switchEditLayoutEdit();
				uptUi.uptUi();
			}
		}
	}
	private void forwardAction(ArrayList<SessionInfo> list)
	{
		Log.debug(this.getClass(),"forwardAction list = " + list);
		Intent intent = new Intent();
		intent.setClass(ctx, ForwardMsgActivity.class);
		intent.putParcelableArrayListExtra(ForwardMsgActivity.KEY,list);
//		intent.putExtra(ForwardMsgActivity.KEY,info);
		//		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		ctx.startActivity(intent);
	}
	private void doCopyAction()
	{
		SessionInfo info = list.get(curPos);
		if(info != null
			&& !info.isFdMsg()) {
			ClipboardManager clipboard = (ClipboardManager) ctx.getSystemService(Context.CLIPBOARD_SERVICE);

			ClipData clip = ClipData.newPlainText("Copied Text", info.getContent());
			clipboard.setPrimaryClip(clip);
			CommDlg.showToast(ctx,R.string.op_msg_copy);
		}
	}
	private void doDelAction()
	{
		delAction(curPos);
		if(uptUi != null)
		{
			uptUi.switchEditLayoutEdit();
			uptUi.uptUi();
		}
		mulSelectList.clear();
		curPos=0;
	}

	private void delAction(int pos)
	{
		SysApplication app = ((SysApplication) ctx.getApplicationContext());
		Product product = app.getProduct();
		if (product != null) {
			MsgOperator operator = (MsgOperator) product.getOperator(ProductConst.Func_Msg);
			if(list.get(pos) != null) {
				SessionInfo info = list.get(pos);
				operator.delSession(info);
			}
		}
	}

	private void delHistory(AddrInfo info)
	{
		SysApplication app = ((SysApplication) ctx.getApplicationContext());
		Product product = app.getProduct();
		if (product != null) {
			MsgOperator operator = (MsgOperator) product.getOperator(ProductConst.Func_Msg);
			operator.delHirstory(info.getAddrId());
		}
	}

	public void doDelMulAction()
	{
		if(mulSelectList != null
			&& mulSelectList.size() > 0)
		{
			Log.debug(this.getClass(),"doDelMulAction mulSelectList = " + mulSelectList);
			boolean isDelHistory = false;
			if(mulSelectList.size() == this.list.size())
			{
				isDelHistory = true;
			}
			for(Iterator<MulSelectedIndex> itor = mulSelectList.iterator();itor.hasNext();)
			{
				MulSelectedIndex index = itor.next();
				Log.debug(this.getClass(),"doDelMulAction index = " + index);
				delAction(index.getIndex());
			}
			if(isDelHistory)
			{
				delHistory(curAddrInfo);
			}
			this.isMulSelected = false;
			if(uptUi != null)
			{
				uptUi.uptUi();
			}
		}
	}

	private void doMulSelectedAction()
	{
		Log.debug(this.getClass(),"doMulSelectedAction");
		this.isMulSelected = true;
		if(uptUi != null)
		{
			uptUi.switchEditLayoutMulSelected();
			uptUi.uptUi();
		}
	}

	public void cancelMulSelectedAction()
	{
		Log.debug(this.getClass(),"doCancelMulSelectedAction");
		this.isMulSelected = false;
		mulSelectList.clear();
		if(uptUi != null)
		{
			uptUi.switchEditLayoutEdit();
			uptUi.uptUi();
		}
	}

	private void removeMulSelectIndex(int pos)
	{
		if(mulSelectList != null
			&& mulSelectList.size() > 0)
		{
			for (Iterator<MulSelectedIndex> itor = mulSelectList.iterator();itor.hasNext();)
			{
				if(itor.next().isSameOne(pos))
				{
					itor.remove();
				}
			}
		}
	}
	private boolean hasMulSelectIndex(int pos)
	{
		if(mulSelectList != null
				&& mulSelectList.size() > 0)
		{
			for (Iterator<MulSelectedIndex> itor = mulSelectList.iterator();itor.hasNext();)
			{
				if(itor.next().isSameOne(pos))
				{
					return true;
				}
			}
		}
		return false;
	}



	private class ViewHolder {
		public CheckBox mulSelectedCheckBox;
		public ImageView imgView;

		public TextView tvSendTime;
//		public TextView funcTitleTv;

		public TextView tvUserName;
		public FrameLayout contentLy;
		public TextView textTv;
		//		public LinearLayout ly_file;
//		public TextView tv_left_len;
		public LinearLayout voiceLy;
		public ImageView voiceIv;
		public TextView voiceLenTv;
		public TextView voiceListenedTv;

		public ImageView imageIv;
		public LinearLayout fileLy;
		public TextView fileNameTv;
		public TextView fileSizeTv;

		public ImageView statusIv;
//		public TextView tv_right_len;
		//		public boolean isComMsg = true;  
	}

}
